package com.atwisdom.star.service.planning;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONArray;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Panhao
 * @ClassName Planning
 * @description TODO
 * @date 2024/6/5
 * lastUpdate Panhao
 */
@Data
public class Planning {
    private Routing routing;
    private List<Product> requirement = new ArrayList<>();
    public void init(JSONArray nodes){
        routing.setRouting(nodes);
    }
    public void setRequirement(){
        routing.getRouting().forEach(workCenter -> {
            requirement.addAll(workCenter.getProduct().getBoms());
        });
    }
    public Map<JSONObject,List<Product>> groupRequirement(){
        return requirement.stream().collect(Collectors.groupingBy(Product::getFeature));
    }
    public Map<JSONObject,Product> calcRequirement(){
        Map<JSONObject,Product> map = new HashMap<>();
        groupRequirement().forEach((k,v) -> {
            v.get(0).setNumber(v.stream().mapToDouble(Product::getNumber).sum());
            map.put(k,v.get(0));
        });
        return map;
    }
}
